package com.bi.bitest.bao;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioGroup;
import com.bi.bitest.bao.fragments.CustomTingFragment;
import com.bi.bitest.bao.fragments.DiscoverFragment;
import com.bi.bitest.bao.fragments.DownloadTingFragment;
import com.bi.bitest.bao.fragments.PersonalFragment;
import com.bi.bitest.bao.service.PlayService;

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    private  RadioGroup radioGroup;
    private Fragment[]fragments;

    private WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    private  WindowManager wm;
    private ImageView imageView;


/*
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
*/


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        wm=getWindowManager();
        imageView=new ImageView(this);
        imageView.setImageResource(R.drawable.play_icon_new);
        WindowManager.LayoutParams lp=new WindowManager.LayoutParams();

        //设置悬浮窗的位置
        lp.gravity= Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM;//参考点
        //x y是相对于gravity设置的为之而言的偏移
        lp.x=0;
        lp.y=0;
        lp.width=WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height=WindowManager.LayoutParams.WRAP_CONTENT;
        //悬浮窗通过addView后如果悬浮窗可以获取焦点那么下层的activity将不能交互
        lp.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        //设置悬浮窗没有内容的区域为透明
        lp.format= PixelFormat.TRANSPARENT;

        wm.addView(imageView, lp);




           //手机型号
        Log.d("Device", Build.MODEL);

    //TODO 检查横竖屏切换时 这个数组对象和内部元素是否置空
        fragments=new Fragment[4];
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction tx = manager.beginTransaction();
        if(savedInstanceState!=null){
            Log.d("xx","yuyu");
            int len=fragments.length;
            for(int i=0;i<len;i++){
                fragments[i]=manager.findFragmentByTag("f"+i);
                tx.hide(fragments[i]);
            }
            tx.show(fragments[0]);

            tx.commit();
        }
        else {
            fragments[0] = new DiscoverFragment();
            fragments[1] = new CustomTingFragment();
            fragments[2] = new DownloadTingFragment();
            fragments[3] = new PersonalFragment();

            int length = fragments.length;
            for (int i = 0; i < length; i++) {
                //添加fragmentss使用tag  fragement重新创建的时候复用
                tx.add(R.id.main_tab_container, fragments[i],"f"+i);
                tx.hide(fragments[i]);

            }
            tx.show(fragments[0]);

            tx.commit();
        }
        radioGroup = (RadioGroup) findViewById(R.id.main_tab_bar_group);
/*              RadioButton button= (RadioButton) radioGroup.getChildAt(0);
        button.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.tab4_down,0,0);
        button.setTextColor(Color.parseColor("#ffff8761"));*/
            radioGroup.setOnCheckedChangeListener(this);

    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int index = 0;
        switch (checkedId){
            case R.id.main_tab_item_discover:
                index = 0;
                break;
            case R.id.main_tab_item_custom:
                index = 1;
                break;
            case R.id.main_tab_item_download:
                index = 2;
                break;
            case R.id.main_tab_item_personal:
                index = 3;
                break;

            default: break;
        }
        int length=fragments.length;
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction tx = fragmentManager.beginTransaction();
        for (int i = 0; i < length; i++) {
            if ((i == index)){
                tx.show(fragments[i]);
            }else{
                tx.hide(fragments[i]);
            }

        }
        tx.commit();
    }


    @Override
    protected void onDestroy() {
        Intent intent=new Intent(MainActivity.this, PlayService.class);
        stopService(intent);

        WindowManager manager=getWindowManager();
        manager.removeViewImmediate(imageView);
        super.onDestroy();
    }
}
